<?php
session_start();
include('functions.php');  
// Define a destination
$targetDir = realpath(dirname(__DIR__)).'\MC_DATA'; // Relative to the root

$targetFolder = str_replace('\\', '/', $targetDir); 

//Asia/Kolkata


if(isset($_POST['type']) && $_POST['type'] == 'photoidcheck') 
{								  
	$verifyToken = md5('unique_salt' . $_POST['timestamp']);

	if (!empty($_FILES) && $_POST['token'] == $verifyToken) { 
		$tempFile = $_FILES['Filedata']['tmp_name']; 
	
		// Validate the file type
		
		$fileParts = pathinfo($_FILES['Filedata']['name']);
		/*print_r($fileParts); 
		exit;*/
		/* ********** */
		
		//$sql = "SELECT FirstName, SurName FROM crm_contact WHERE ContactID = ".$_SESSION['CurrentContact_id'].";";
		//echo $sql;
		//$res = mysql_query($sql);
		//$row_set = mysql_fetch_assoc($res);
		$tmp = $_FILES["Filedata"]["tmp_name"];
		$created_time = time();
		//$file = 'ID_'.date('YmdHsi').'_'.$row_set['SurName'].'_'.$row_set['FirstName'].'_'.$_SESSION['app_id'].'.'.$fileParts['extension'];
		$file = 'ID_'.getDateFormat($created_time, 'YmdHsi').'_'.$fileParts['filename'].'_'.$_SESSION['app_id'].'.'.$fileParts['extension'];
		//$file_path = $_SERVER['DOCUMENT_ROOT']."/draft/MC_DATA/IDCheck/".$file;
		$file_path = $targetFolder."/IDCheck/".$file;
		//$file_path = "maxcash/MC_DATA/IDCheck/".$file;
		$store_file_path = "MC_DATA/IDCheck/".$file;			
		/* ********** */		
		$fileTypes = array('jpg', 'jpeg', 'gif', 'png', 'doc', 'docx', 'pdf'); // Allowed file extensions
		if (in_array(strtolower($fileParts['extension']), $fileTypes)) {
			if(move_uploaded_file($_FILES["Filedata"]["tmp_name"], $file_path))
			{
				$_SESSION['saved_files'][] = array('file' => $file,
												   'file_name' => $_FILES['Filedata']['name'],
												   'file_path' => $file_path,
												   'stored_path' => $store_file_path,
												   'created_time' => $created_time
												   );
				
				echo 'Success';
			}
			else
			{
				echo 'Failure';
			}
		}
		else
		{
			echo 'Invalid';
		}
			//echo '1';
		
		
	}
}

if(isset($_POST['type']) && $_POST['type'] == 'photoiddelete')
{	
	$arr_index;
	$filename = $_POST['filename'];
	if(isset($_SESSION['saved_files']) && !empty($_SESSION['saved_files']))
	{
		$files = $_SESSION['saved_files'];
		
		//print_r($files);
		
		foreach($files as $index => $file)
		{
			if($file['file_name'] == $filename)
			{
				$arr_index = $index;
			}
		}
		
			if(file_exists($files[$arr_index]['file_path']))
			{
				unlink($files[$arr_index]['file_path']);
			}
			unset($_SESSION['saved_files'][$arr_index]);
			echo $filename.' has been deleted successfully';
		
	}
}

if(isset($_POST['type']) && $_POST['type'] == 'payslipcheck')
{	
	$verifyToken = md5('unique_salt' . $_POST['timestamp']);

	if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
		$tempFile = $_FILES['Filedata']['tmp_name'];
		
		
		// Validate the file type
		$fileParts = pathinfo($_FILES['Filedata']['name']);
		
		
		/* ********** */
		//ID2013120101035_2013_Testuser_3.png
		//$sql = "SELECT FirstName, SurName FROM crm_contact WHERE ContactID = ".$_SESSION['CurrentContact_id'].";";
		//echo $sql;
		//$res = mysql_query($sql);
		//$row_set = mysql_fetch_assoc($res);
		$tmp = $_FILES["Filedata"]["tmp_name"];
		$created_time = time();
		//$file = 'PS_'.date('YmdHsi').'_'.$row_set['SurName'].'_'.$row_set['FirstName'].'_'.$_SESSION['app_id'].'.'.$fileParts['extension'];
		$file = 'PS_'.getDateFormat($created_time, 'YmdHsi').'_'.$fileParts['filename'].'_'.$_SESSION['app_id'].'.'.$fileParts['extension'];
		//$file_path = $_SERVER['DOCUMENT_ROOT']."/draft/MC_DATA/PaySlipCheck/".$file;
		$file_path = $targetFolder."/PaySlipCheck/".$file;
		$store_file_path = "MC_DATA/PaySlipCheck/".$file;			
		/* ********** */
		
		
			//move_uploaded_file($tempFile,$file_path);
		$fileTypes = array('jpg', 'jpeg', 'gif', 'png', 'doc', 'docx', 'pdf'); // Allowed file extensions
		if (in_array(strtolower($fileParts['extension']), $fileTypes)) {
			if(move_uploaded_file($_FILES["Filedata"]["tmp_name"], $file_path))
			{ 
				$_SESSION['saved_files'][] = array('file' => $file,
												   'file_name' => $_FILES['Filedata']['name'],
												   'file_path' => $file_path,
												   'stored_path' => $store_file_path,
												   'created_time' => $created_time
												   );
				echo 'Success';
			}
			else
			{
				echo 'Failure';
			}
		}
		else
		{
			echo 'Invalid';
		}
			
		
			//echo '1';
		
	}
} 
if(isset($_POST['type']) && $_POST['type'] == 'payslipdelete')
{	
	$arr_index; 
	$filename = $_POST['filename'];
	if(isset($_SESSION['saved_files']) && !empty($_SESSION['saved_files']))
	{
		$files = $_SESSION['saved_files'];
		
		//print_r($files);
		 
		foreach($files as $index => $file)
		{
			if($file['file_name'] == $filename)
			{
				$arr_index = $index;
			}
		}
		
			if(file_exists($files[$arr_index]['file_path']))
			{
				unlink($files[$arr_index]['file_path']);
			}
			unset($_SESSION['saved_files'][$arr_index]);
			echo $filename.' has been deleted successfully';
		
	}
}

if(isset($_POST['subject']) && $_POST['subject'] == 'e-sign')
{
	
}

function formatbytes($file, $type)
{
   switch($type){
      case "KB":
         $filesize = filesize($file) * .0009765625; // bytes to KB
      break;
      case "MB":
         $filesize = (filesize($file) * .0009765625) * .0009765625; // bytes to MB
      break;
      case "GB":
         $filesize = ((filesize($file) * .0009765625) * .0009765625) * .0009765625; // bytes to GB
      break;
   }
   if($filesize <= 0){
      return $filesize = 'unknown file size';}
   else{return round($filesize, 2).' '.$type;}
}

?>